package com.kx.center.util;

import java.util.Random;

public class SerialNumber {
	
	public static String ALLOCATION = "AL";
	public static String DAILY_COUNTING = "DC";
	public static String ORDER = "OD";
	public static String STOCKIN = "SI";
	public static String ORDER_NUM = "ON";
	public static String PAY_NUM = "PN";	
	public static String REFUND_NUM = "RN";
	
	public static String getRandomString(int length) {
	    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 } 
	
	public static String getSN(String type, Integer storeId) {
		long timestamp = System.currentTimeMillis();
		return type + convert((long)storeId) + convert(timestamp);
	}
	
	private static String convert(Long val) {

		StringBuilder builder = new StringBuilder();
		
		int right = 0;
		
		do {
			right = (int) (val % 36);
			val = (long) Math.floor((double)val / 36);
			builder.insert(0, getString(right));
		} while(val >0);
		
		int len = builder.length();
		
		while(len++ < 4) {
			builder.insert(0, '0');
		}
		
		return builder.toString();
	}
	
	private static String getString(Integer val) {
		if(val < 10)
			return val.toString();
		else 
			return String.valueOf(Character.toChars(val + 55));
	}
	
}
